/**
 * Copyright ©2016-2020 northkingbpo Corporation, All Rights Reserved
 * http://www.northkingbpo.net/
 */
package profile.hujia.algorithm.common;

import java.util.Map;
import java.util.Random;

/**
 * class info
 *
 * @author hujia
 * @date 2019-01-29 15:37
 */
public class ParticleSwarmOptimizationFunction {
    /**
     * check the number is weather satisfy the rule.
     *
     * @return true express >=2 or number is 0, false express <2
     */
    public static boolean checkUserConstraint(int number, Map<Integer, Integer> constraintUser) {
        if (number == 0) {
            return false;
        }
        Integer result = constraintUser.get(number);
        if (result == null) {
            constraintUser.put(number, 1);
            return false;
        } else if (result < Constant.MAX_TASK_NUMBER) {
            constraintUser.put(number, result + 1);
            return false;
        }
        return true;
    }

    public static void adjustFeasibleSolution(int[] feasibleSolutionBranch, Map<Integer, Integer> constraintUser,
                                              int[][] constraintCondition,int taskNumber) {
        Random random = new Random();
        int k = 0;
        //task current position
        int currentPosition;
        int begin = random.nextInt(taskNumber);
        constraintUser.clear();
        while (k < taskNumber) {
            currentPosition = (k + begin) % taskNumber;
            int index = feasibleSolutionBranch[currentPosition];
            int indexLength = constraintCondition[currentPosition].length;
            if (index < 0) {
                feasibleSolutionBranch[currentPosition] = 0;
                index = 0;
            } else if (index >= indexLength) {
                feasibleSolutionBranch[currentPosition] = indexLength - 1;
                index = indexLength - 1;
            }
            int adjustCount = 0;
            while (checkUserConstraint(constraintCondition[currentPosition][index], constraintUser) && adjustCount < indexLength - 1) {
                index = (index + 1) % (indexLength);
                if (index == 0) {
                    index = 1;
                }
                ++adjustCount;
            }
            if (adjustCount == indexLength - 1) {
                feasibleSolutionBranch[currentPosition] = 0;
            } else {
                feasibleSolutionBranch[currentPosition] = index;
            }
            ++k;
        }
    }

    public static Integer getScore(int[] feasibleSolution, int[][] constraintCondition, Map<String, Integer> score,int taskNumber) {

        int totalScore = 0;
        for (int i = 0; i < taskNumber; ++i) {
            if (feasibleSolution[i] == 0) {
                continue;
            }
//            int userId = constraintConditionSpecific[i][feasibleSolution[i]];
//            String key = String.format("%04d", userId) + String.format("%04d", i + 1);
//            totalScore += score.get(key);
            ++totalScore;
        }
        return totalScore;
    }
}
